package com.yan.club.auth.autoconfiration;

import com.yan.club.auth.component.MySecurityExpressionRoot;
import com.yan.club.auth.filter.JwtAuthenticationTokenFilter;
import com.yan.club.auth.handler.AccessDeniedHandlerImpl;
import com.yan.club.auth.handler.AuthenticationEntryPointImpl;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import org.springframework.context.annotation.Lazy;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.config.http.SessionCreationPolicy;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter;

import javax.annotation.Resource;

/**
 * @author 彦
 * @since 2023/12/26 13:56
 */
@Configuration
@EnableWebSecurity
//@ConditionalOnMissingBean
@Import({JwtAuthenticationTokenFilter.class, MySecurityExpressionRoot.class})
public class ClubAuthAutoConfiguration extends WebSecurityConfigurerAdapter {

    @Resource
    private JwtAuthenticationTokenFilter jwtAuthenticationTokenFilter;

    @Bean
    @ConditionalOnMissingBean
    public PasswordEncoder passwordEncoder() {
        return new BCryptPasswordEncoder();
    }

    @Bean
    @Override
    @ConditionalOnMissingBean
    @Lazy
    public AuthenticationManager authenticationManagerBean() throws Exception {
        return super.authenticationManagerBean();
    }

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        // 关闭自带的logout请求
        http.logout().disable();

        http
                //关闭csrf
                .csrf().disable()
                //不通过Session获取SecurityContext(存储认证后的信息)
                .sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS)
                .and()
                .authorizeRequests()
                // 对于登录接口 允许匿名访问(未登录可以访问，登录了不能访问)
                .antMatchers("/sys/user/login").anonymous()
                .antMatchers("/sys/user/logout").permitAll()
                // 除上面外的所有请求全部需要鉴权认证
                .anyRequest().authenticated();

        //允许跨域
        http.cors();

//         添加过滤器 改造过滤器链 （认证token）
        http.addFilterBefore(jwtAuthenticationTokenFilter,
                UsernamePasswordAuthenticationFilter.class);

//         配置异常处理器
        http.exceptionHandling()
                // 配置认证失败处理器
                .authenticationEntryPoint(new AuthenticationEntryPointImpl())
                // 配置授权失败处理器
                .accessDeniedHandler(new AccessDeniedHandlerImpl());
    }
}
